<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Partial function evaluation &#8212; Boost.HigherOrderFunctions 0.6 documentation</title>
    
    <link rel="stylesheet" href="../../_static/boostbook.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '0.6',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="FAQ" href="faq.html" />
    <link rel="prev" title="Discussion" href="discussion.html" /> 
  </head>
  <body role="document">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86"src="../../_static/boost.png"></td>
</tr></table>

<div class="spirit-nav">
<a accesskey="p"  href="discussion.html"><img src="../../_static/prev.png" alt="Prev"></a>
<a accesskey="u" href="discussion.html"><img src="../../_static/up.png" alt="Up"></a>
<a accesskey="h" href="../index.html"><img src="../../_static/home.png" alt="Home"></a>
<a accesskey="n"  href="faq.html"><img src="../../_static/next.png" alt="Next"></a>
</div>
  

    <div class="document">
  <div class="chapter">
      <div class="body" role="main">
        
  <!-- Copyright 2018 Paul Fultz II
     Distributed under the Boost Software License, Version 1.0.
     (http://www.boost.org/LICENSE_1_0.txt)
--><div class="section" id="partial-function-evaluation">
<h1>Partial function evaluation<a class="headerlink" href="#partial-function-evaluation" title="Permalink to this headline">¶</a></h1>
<p>Many of the adaptors(such as <a class="reference internal" href="../../include/boost/hof/partial.html#partial"><span class="std std-ref">partial</span></a> or <a class="reference internal" href="../../include/boost/hof/pipable.html#pipable"><span class="std std-ref">pipable</span></a>) in the library supports optional partial evaluation of functions. For example, if we have the <code class="docutils literal"><span class="pre">sum</span></code> function adapted with the <code class="docutils literal"><span class="pre">partial</span></code> adaptor:</p>
<div class="highlight-cpp"><div class="highlight"><pre><span class="k">auto</span> <span class="n">sum</span> <span class="o">=</span> <span class="n">partial</span><span class="p">([](</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="n">x</span><span class="o">+</span><span class="n">y</span><span class="p">;</span>
<span class="p">});</span>
</pre></div>
</div>
<p>So if we write <code class="docutils literal"><span class="pre">sum(1,</span> <span class="pre">2)</span></code> it will return 3, however, if we write <code class="docutils literal"><span class="pre">sum(1)</span></code> it will return a new function, which when called again, it will evaluate the function and return 3:</p>
<div class="highlight-cpp"><div class="highlight"><pre><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">sum</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">);</span> <span class="c1">// Returns 3</span>
<span class="k">auto</span> <span class="n">f</span> <span class="o">=</span> <span class="n">sum</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span>
<span class="kt">int</span> <span class="n">j</span> <span class="o">=</span> <span class="n">f</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span> <span class="c1">// returns 3</span>
</pre></div>
</div>
<p>Of course due to limitations in C++, deciding whether evaluate the function or to partially evaluated it, is based on the callability of the function and not arity. So if we call <code class="docutils literal"><span class="pre">sum(1,</span> <span class="pre">2,</span> <span class="pre">3)</span></code>, it will return a function:</p>
<div class="highlight-cpp"><div class="highlight"><pre><span class="k">auto</span> <span class="n">f</span> <span class="o">=</span> <span class="n">sum</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">);</span>
</pre></div>
</div>
<p>However, this can get out of hande as the function <code class="docutils literal"><span class="pre">f</span></code> will never be evaluated. Plus, it would be nice to produce an error at the point of calling the function rather than a confusing error of trying to use a partial function. The <a class="reference internal" href="../../include/boost/hof/limit.html#limit"><span class="std std-ref">limit</span></a> decorator lets us annotate the function with the max arity:</p>
<div class="highlight-cpp"><div class="highlight"><pre><span class="k">auto</span> <span class="n">sum</span> <span class="o">=</span> <span class="n">partial</span><span class="p">(</span><span class="n">limit_c</span><span class="o">&lt;</span><span class="mi">2</span><span class="o">&gt;</span><span class="p">([](</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="n">x</span><span class="o">+</span><span class="n">y</span><span class="p">;</span>
<span class="p">}));</span>
</pre></div>
</div>
<p>So now if we call <code class="docutils literal"><span class="pre">sum(1,</span> <span class="pre">2,</span> <span class="pre">3)</span></code>, we will get a compiler error. So this improves the situation, but it is not without its limitations. For example if we were to define a triple sum using the <a class="reference internal" href="../../include/boost/hof/pipable.html#pipable"><span class="std std-ref">pipable</span></a> adaptor:</p>
<div class="highlight-cpp"><div class="highlight"><pre><span class="k">auto</span> <span class="n">sum</span> <span class="o">=</span> <span class="n">pipable</span><span class="p">(</span><span class="n">limit_c</span><span class="o">&lt;</span><span class="mi">3</span><span class="o">&gt;</span><span class="p">([](</span><span class="kt">int</span> <span class="n">x</span><span class="p">,</span> <span class="kt">int</span> <span class="n">y</span><span class="p">,</span> <span class="kt">int</span> <span class="n">z</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="n">x</span><span class="o">+</span><span class="n">y</span><span class="o">+</span><span class="n">z</span><span class="p">;</span>
<span class="p">}));</span>
</pre></div>
</div>
<p>So if we call <code class="docutils literal"><span class="pre">sum(1)</span></code>, there is no compiler error, not until we try to pipe in a value:</p>
<div class="highlight-cpp"><div class="highlight"><pre><span class="k">auto</span> <span class="n">f</span> <span class="o">=</span> <span class="n">sum</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span> <span class="c1">// No error here</span>
<span class="k">auto</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">|</span> <span class="n">f</span><span class="p">;</span> <span class="c1">// Compile error</span>
</pre></div>
</div>
<p>Of course, the goal may not be to use the pipable call, which could lead to some confusing errors. Currently, there is not a good solution to this.</p>
</div>


      </div>
  </div>
      <div class="clearer"></div>
    </div>
    <div class="footer" role="contentinfo">
    <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
    <td align="left"></td>
    <td align="right"><div class="copyright-footer">
            &#169; Copyright 2016, Paul Fultz II.
        
          Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.6.
          <p>Distributed under the Boost Software License, Version 1.0.
          (See accompanying file <code class="filename">LICENSE_1_0.txt</code> or copy at 
          <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
          </p>
    </div></td>
    </tr></table>
    </div>
  </body>
</html>